package com.webserver.controller;

import com.webserver.entity.User;
import com.webserver.http.HttpServeletRequest;
import com.webserver.http.HttpServeletResponse;

import java.io.*;
import java.net.URISyntaxException;

public class UserController {
    private static File users;
    private static File root;
    private static File staticDir;
    static {
        try {
            if (!users.exists()){
                users.mkdirs();
            }
            root = new File(UserController.class.getClassLoader().getResource(".").toURI());
            staticDir = new File(root, "static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public void reg(HttpServeletRequest request, HttpServeletResponse response){
        System.out.println("开始处理用户注册");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String agestr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+agestr);
        if (username==null||username.isEmpty()||password==null||password.isEmpty()||
                nickname==null||nickname.isEmpty()||agestr==null||agestr.isEmpty()
        ){
            response.sendRedirect("/reg_infor_error.html");
            return;
        }
        try {
            int age = Integer.getInteger(agestr);
            User user = new User(username,password,nickname,age);
            File file = new File(users,username+".obj");
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(user);
            response.sendRedirect("/reg_success");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void login(HttpServeletRequest request,HttpServeletResponse response){
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
    }
}
